﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcMasterPage.Helper
{
    /// <summary>
    /// 用户验证列举
    /// </summary>
    [Flags]
    public enum RoleFlag
    {
        /// <summary>
        /// 登录的用户 
        /// </summary>
        User = 1,
        /// <summary>
        /// 工作室老师
        /// </summary>
        WorkShopTeacher = 2,
        /// <summary>
        /// 工作室学生
        /// </summary>
        WorkShopUser = 4,
        /// <summary>
        /// 专家老师
        /// </summary>
        SpecialTeacher = 8,
        /// <summary>
        /// 版主
        /// </summary>
        Admin = 16,
    }

    /// <summary>
    /// 角色过滤器
    /// </summary>
    public class RoleFilter : AuthorizeAttribute
    {
        #region Constructs
        /// <summary>
        /// 角色过滤器构造依法
        /// </summary>
        /// <param name="roleFlag">角色类型</param>
        public RoleFilter(RoleFlag roleFlag)
        {
            _RoleFlag = roleFlag;
        }
        #endregion

        #region Consts
        /// <summary>
        /// 必须登陆警告
        /// </summary>
        const string USER_WARN = "您必须先登陆才能执行本操作";
        /// <summary>
        /// 必须是工作室成员警告
        /// </summary>
        const string WORKSHOPUSER_WARN = "必须是本工作室的用户才能执行本操作";
        /// <summary>
        /// 必须是工作室老师警告
        /// </summary>
        const string WORKSHOPTEACHER_WARN = "必须是本工作室的老师才能执行本操作";
        #endregion

        #region Fields
        /// <summary>
        /// 同步方式
        /// </summary>
        string htmlMsg = @"<html><head><title></title></head><body>
                               <link href='/Scripts/jquery-plugin-boxy/css/boxy.css' rel='stylesheet' type='text/css' /> 
                               <script src='/Scripts/jquery-1.4.4.min.js' type='text/javascript'></script>
                               <script src='/Scripts/jquery-plugin-boxy/js/jquery.boxy.js' type='text/javascript'></script>
                               <script src='/Scripts/jquery-plugin-boxy/jBoxyFun.js' type='text/javascript'></script>
　                             <script type='text/javascript'>
                                $(function(){
                                 alertHrefFun('msg','url');});</script></body></html>";
        /// <summary>
        /// 异步方式
        /// </summary>
        string jsMsg = @"alertHrefFun('msg','url');";

        RoleFlag _RoleFlag;
        #endregion

        /// <summary>
        /// 根据请求上下文类型，返回对象
        /// </summary>
        /// <param name="request"></param>
        /// <param name="jsMsg"></param>
        /// <param name="htmlMsg"></param>
        /// <returns></returns>
        ActionResult GetContentResult(HttpRequestBase request, string Warn)
        {
            var result = new ContentResult
            {
                Content = htmlMsg.Replace("msg", Warn).Replace("url", request.UrlReferrer.ToString()),
                ContentType = "text/html",
            };
            if (request.IsAjaxRequest())
            {
                result.Content = jsMsg.Replace("msg", Warn).Replace("url", request.UrlReferrer.ToString());
                result.ContentType = "text/javascript";
            }
            return result;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var userID = filterContext.RequestContext.HttpContext.Session["UserID"];
            var request = filterContext.RequestContext.HttpContext.Request;
            var routeData = filterContext.RequestContext.RouteData.Values["id"];
            switch (this._RoleFlag)
            {
                case RoleFlag.User:
                    if (userID == null || Convert.ToInt32(userID) <= 0)
                    {
                        filterContext.Result = GetContentResult(request, USER_WARN);
                    }

                    break;
                default:
                    break;
            }
        }
    }
}